package com.offcn.web;

import com.offcn.entity.CourseUser;
import com.offcn.service.CourseUserService;
import com.offcn.service.CourseUserServiceImpl;
import com.offcn.utils.BaseServlet;
import com.offcn.utils.JsonUtils;
import com.offcn.utils.PageUtils;
import com.offcn.utils.ResultVO;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/courseUser")
public class CourseUserServlet extends BaseServlet {
    CourseUserService courseUserService = new CourseUserServiceImpl();
    ResultVO resultVO = null;
    CourseUser courseUser = new CourseUser();

    //分页
    public void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取查询的关键字，当前页码 页量
        String search = req.getParameter("search");
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        PageUtils<CourseUser> pageUtils=courseUserService.find(search,currentPage,pageSize);
        resultVO = new ResultVO(200,"查询成功",pageUtils);
        JsonUtils.toPrint(resultVO,resp);
    }

    //修改
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        int id = Integer.parseInt(req.getParameter("id"));
        int cid = Integer.parseInt(req.getParameter("cid"));

        Boolean b=courseUserService.updateCourse(id,cid);
        if (b){
            resultVO=new ResultVO(200,"修改成功",null);
        }else {
            resultVO=new ResultVO(500,"修改失败",null);
        }
        JsonUtils.toPrint(resultVO,resp);

    }

    //删除
    public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        int uid = Integer.parseInt(req.getParameter("uid"));

        Boolean b=courseUserService.del(uid);
        if (b){
            resultVO=new ResultVO(200,"删除成功",null);
        }else {
            resultVO=new ResultVO(500,"删除失败",null);
        }
        JsonUtils.toPrint(resultVO,resp);

    }
}
